//
//  TGDealInfoController.m
//  GroupBuy
//
//  Created by wwj on 15/11/12.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGDealInfoController.h"
#import "TGInfoHeaderView.h"
#import "TGHttpRequest.h"
#import "TGDealModel.h"
#import "TGInfoTextView.h"

#define kWidth 430

@interface TGDealInfoController ()
@property (weak, nonatomic) UIScrollView *scrollerView;
@end

@implementation TGDealInfoController

-(void)setModel:(TGDealModel *)model{
    _model = model;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBgColor;
    
    //添加滚动视图
    [self addScrollView];
    //添加头部控件
    [self addHeaderView];
    [self loadData];
    
}

-(void)loadData{
    [TGHttpRequest getGroupDetailWithDealId:_model.deal_id success:^(id data, id totalCount) {
        _model = data;
        [self addDetail];
        
    } error:nil];
}

-(void)addDetail{
    //团购详情
    [self addInfoTextWithTitle:@"团购详情" image:@"ic_content.png" content:_model.details];
    //购买须知
    [self addInfoTextWithTitle:@"购买须知" image:@"ic_tip.png" content:_model.restrictions.special_tips];
    
    [self addInfoTextWithTitle:@"重要通知" image:@"ic_tip.png" content:_model.notice];
}

#pragma mark 添加滚动视图
- (void)addScrollView{
    UIScrollView *scrollerView = [[UIScrollView alloc] init];
    scrollerView.bounds = CGRectMake(0, 0, kWidth, self.view.frame.size.height);
    scrollerView.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height * 0.5);
    scrollerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    scrollerView.contentInset = UIEdgeInsetsMake(85, 0, 0, 0);
    [self.view addSubview:scrollerView];
    _scrollerView = scrollerView;
}

#pragma mark 添加头部控件
- (void)addHeaderView{
    TGInfoHeaderView *headView = [TGInfoHeaderView infoHeaderView];
    headView.frame = CGRectMake(0, 0, _scrollerView.frame.size.width, headView.frame.size.height);
    headView.model = _model;
    [_scrollerView addSubview:headView];
    _scrollerView.contentSize = CGSizeMake(0, CGRectGetMaxY(headView.frame));
}

-(void)addInfoTextWithTitle:(NSString *)title image:(NSString *)image content:(NSString *)content{
    if (content == nil || content.length <=0) {
        return;
    }
    TGInfoTextView *infoTextView = [TGInfoTextView infoTextView];
    infoTextView.frame = CGRectMake(0, _scrollerView.contentSize.height, kWidth, 0);
    infoTextView.icon = image;
    infoTextView.title = title;
    infoTextView.content = content;
    
    [_scrollerView addSubview:infoTextView];
    _scrollerView.contentSize = CGSizeMake(0, CGRectGetMaxY(infoTextView.frame)+ 20);
}

@end
